package com.kyb.impl;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import com.kyb.dao.IUserDao;
import com.kyb.entity.User;
import com.kyb.util.BaseDB;



public class UserDaoImpl extends BaseDB implements IUserDao {
	Connection conn = null;
	PreparedStatement pstmt = null;
	ResultSet rs = null;

	public boolean addUser(User user) {
		try {
			conn = getConn();
			String sql = "insert into user"
					+ "(user_id,name,union_id,ding_id,open_id,mobile,active,remark,avatar,"
					+ "isadmin,isHide,isboss,isSenior"
					+ ",tel,position,department,workplace,email) "
					+ "values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, user.getUserId());
			pstmt.setString(2, user.getName());
			pstmt.setString(3, user.getUnionId());
			pstmt.setString(4, user.getDingId());
			pstmt.setString(5, user.getOpenId());
			pstmt.setString(6, user.getMobile());
			pstmt.setString(7, user.getActive());
			pstmt.setString(8, user.getRemark());
			pstmt.setString(9, user.getAvatar());
			pstmt.setBoolean(10, user.isAdmin());
			pstmt.setBoolean(11, user.isHide());
			pstmt.setBoolean(12, user.isBoss());
			pstmt.setBoolean(13, user.isSenior());
			pstmt.setString(14, user.getTel());
			pstmt.setString(15, user.getPosition());
			pstmt.setString(16, setDeptInfo(user));
			pstmt.setString(17, user.getWorkPlace());
			pstmt.setString(18, user.getEmail());
			
			int flag = pstmt.executeUpdate();

			if (flag > 0) {
				return true;
			}

		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			closeAll(conn, pstmt, rs);
		}
		
		return false;
	}

	@Override
	public boolean deleteUser(String userId) {
		try {
			conn = getConn();
			String sql = "delete from user where user_id = ?";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1,userId);
			int flag = pstmt.executeUpdate();

			if (flag > 0) {
				return true;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			closeAll(conn, pstmt, rs);
		}
		return false;
	}
	
	/**
	 * 获取用户部门信息
	 * @param user
	 * @return
	 */
	private static String setDeptInfo(User user) {
		List<Long> deptlist = user.getDepartment();
		String s1 = "" ;
		int temp = 0;
		for (long long1 : deptlist) {
			if(temp < deptlist.size()-1) {
				s1 += long1+",";
				
			}else {
				s1 += long1;
			}
			++temp;
			
		}
		s1 = s1.substring(0, s1.length());
		return s1;
	}

	@Override
	public boolean updateUser(User user) {
		try {
			conn = getConn();
			String sql = "update user set name=?,mobile=?,active=?,remark=?,avatar=?,isadmin=?,isHide=?,isboss=?,isSenior=?,tel=?,position=?,department=?,workplace=?,email=? where user_id =?";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, user.getName());
			pstmt.setString(2, user.getMobile());
			pstmt.setString(3, user.getActive());
			pstmt.setString(4, user.getRemark());
			pstmt.setString(5, user.getAvatar());
			pstmt.setBoolean(6, user.isAdmin());
			pstmt.setBoolean(7, user.isHide());
			pstmt.setBoolean(8, user.isBoss());
			pstmt.setBoolean(9, user.isSenior());
			pstmt.setString(10, user.getTel());
			pstmt.setString(11, user.getPosition());
			pstmt.setString(12, setDeptInfo(user));
			pstmt.setString(13, user.getWorkPlace());
			pstmt.setString(14, user.getEmail());
			pstmt.setString(15, user.getUserId());
			
			int flag = pstmt.executeUpdate();

			if (flag > 0) {
				return true;
			}

		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			closeAll(conn, pstmt, rs);
		}
		
		return false;
		
	}

	@Override
	public List<User> getAllUser() {
		List<User> list = new ArrayList<User>();
		
		try {
			conn = getConn();
			String sql = "select user_id,name,open_id,mobile,remark,isadmin,isHide,isboss,isSenior,position,department,workplace,email from user";
			pstmt = conn.prepareStatement(sql);
			rs = pstmt.executeQuery();
			while (rs.next()) {
				User user= new User();
				user.setUserId(rs.getString(1));
				user.setName(rs.getString(2));
				user.setOpenId(rs.getString(3));
				user.setMobile(rs.getString(4));
				user.setRemark(rs.getString(5));
				if(("1").equals(rs.getString(6))) {
					user.setAdmin(true);	
				}else {
					user.setAdmin(false);	
				}
				if(("1").equals(rs.getString(7))) {
					user.setHide(true);	
				}else {
					user.setHide(false);	
				}
				if(("1").equals(rs.getString(8))) {
					user.setBoss(true);	
				}else {
					user.setBoss(false);	
				}
				if(("1").equals(rs.getString(9))) {
					user.setSenior(true);	
				}else {
					user.setSenior(false);	
				}
				user.setPosition(rs.getString(10));
				String dept = rs.getString(11);
				List<Long> listIds = Arrays.asList(dept.split(",")).stream().map(s -> Long.parseLong(s.trim())).collect(Collectors.toList());
				user.setDepartment(listIds);
				user.setWorkPlace(rs.getString(12));
				user.setEmail(rs.getString(13));
				list.add(user);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
		
	}

}
